/*
 * CAN_service.c
 *
 *  Created on: 2023骞�10鏈�17鏃�
 *      Author: zcx
 */
#include <HAL.h>
#include <service.h>
#include <manage.h>
#include "string.h"
#pragma CODE_SECTION(EPWM7_ISR, ".TI.ramfunc");
/* *****************************************************************瀹忓畾涔�*************************************************************/
#define CANRXENABLEBIT      0    /*!<閭鏂瑰悜锛氭帴鏀�*/
#define CANTXENABLEBIT      1    /*!<閭鏂瑰悜锛氬彂閫�*/

#define STANDARDFRAME       0    /*!<甯х被鍨嬶細鏍囧噯甯�,ID闀垮害11浣�*/
#define EXTENDFRAME         1    /*!<甯х被鍨嬶細鎵╁睍甯�,ID闀垮害29浣�*/

#define MB_DISABLE          0    /*!<閭绂佽兘*/
#define MB_ENABLE           1    /*!<閭浣胯兘*/

/* ***************************************************************鍐呴儴鍏ㄥ眬鍙橀噺*********************************************************/
static volatile bool GUIMsgWriteEnableFlag  = true;  /*!< GUI鎺ユ敹鏁版嵁鐢ㄦ埛閭鍐欎娇鑳芥爣蹇椾綅*/
static volatile bool GUIMsgReadEnableFlag   = false; /*!< GUI鎺ユ敹鏁版嵁鐢ㄦ埛閭璇讳娇鑳芥爣蹇椾綅*/
static volatile bool GUIOLMsgReadEnableFlag   = false;
static volatile bool GUIOLMsgWriteEnableFlag  = true;  /*!< GUI鎺ユ敹鏁版嵁鐢ㄦ埛閭鍐欎娇鑳芥爣蹇椾綅*/
static volatile bool CellCurrMsgWriteEnableFlag = true;
static volatile bool CloseCellMsgReadEnableFlag   = false;
static volatile bool CellCurrMsgReadEnableFlag = false;
static volatile bool CloseCellMsgWriteEnableFlag  = true;
static volatile bool CELLMsgWriteEnableFlag  = true;  /*!< CELL鎺ユ敹鏁版嵁鐢ㄦ埛閭鍐欎娇鑳芥爣蹇椾綅*/
static volatile bool CELL3MsgReadEnableFlag   = false; /*!< CELL鎺ユ敹鏁版嵁鐢ㄦ埛閭璇讳娇鑳芥爣蹇椾綅*/
static CAN_Data_Type GUIMsg;        /*!<0x710鏁版嵁缂撳瓨*/
static CAN_Data_Type CELL3Msg;        /*!<0x490鏁版嵁缂撳瓨*/
static CAN_Data_Type OLMsg;        /*!<0x705鏁版嵁缂撳瓨*/
static CAN_Data_Type CloseMsg;        /*!<0x705鏁版嵁缂撳瓨*/
static CAN_Data_Type CurrMsg;        /*!<0x705鏁版嵁缂撳瓨*/
// 鍐匔AN鎺ユ敹鍙橀噺瀹氫箟锛堜笌鐜版湁鍙橀噺瀹氫箟浣嶇疆鐩稿悓锛�
/* ***************************************************************鍐呴儴鍑芥暟澹版槑*********************************************************/
/*!
 * @brief 瀵笴AN閭鐨処D銆佹帴鏀剁被鍨嬨�佸抚绫诲瀷杩涜鍒濆鍖�
 * @param MbNum: 閭缂栧彿
 * @param MsgID: CAN ID
 * @param MsgKind:閭鏂瑰悜 鎺ユ敹
 * @param FrameKind:甯х被鍨�
 * @return 鏃�
 */
static void ConfigureMailBox(Uint16 MbNum, Uint32 MsgID, Uint16 MsgKind, Uint16 FrameKind);

/*!
 * @brief 瀵笴AN閭鐨処D銆佸彂閫佺被鍨嬨�佸抚绫诲瀷杩涜鍒濆鍖�
 * @param MbNum: 閭缂栧彿
 * @param MsgID: CAN ID
 * @param MsgKind:閭鏂瑰悜 鍙戦��
 * @param FrameKind:甯х被鍨�
 * @return 鏃�
 */
static void ConfigureMailBoxTransmit(Uint16 MbNum, Uint32 MsgID, Uint16 MsgKind, Uint16 FrameKind);
/* ***************************************************************鍐呴儴鍑芥暟瀹氫箟*********************************************************/
/*!
 * @brief 瀵笴AN閭鐨処D銆佹帴鏀剁被鍨嬨�佸抚绫诲瀷杩涜鍒濆鍖�
 * @param MbNum: 閭缂栧彿
 * @param MsgID: CAN ID
 * @param MsgKind:閭鏂瑰悜 鎺ユ敹
 * @param FrameKind:甯х被鍨�
 * @return 鏃�
 */
static void ConfigureMailBox(Uint16 MbNum, Uint32 MsgID, Uint16 MsgKind, Uint16 FrameKind)    //閰嶇疆鍙戦�丮B锛屾病鏈夋帴鏀�
{
    struct CAN_REGS CanaShadow;

    while (CanaRegs.CAN_IF2CMD.bit.Busy == 0x1u)
    {

    }

    CanaShadow.CAN_IF2MSK.all   = 0x0;          //
    CanaRegs.CAN_IF2MSK.all     = CanaShadow.CAN_IF2MSK.all;

    CanaShadow.CAN_IF2ARB.all           = 0x0;
    CanaShadow.CAN_IF2ARB.bit.Dir       = MsgKind;          //1 for Recieve
    CanaShadow.CAN_IF2ARB.bit.MsgVal    = MB_ENABLE;        //enable the Message Object  Message Valid
    CanaShadow.CAN_IF2ARB.bit.Xtd       = FrameKind;        //Standard/Extend frame
    if (0u != FrameKind)
    {
        CanaShadow.CAN_IF2ARB.bit.ID = MsgID;           //Extend frame  1     If an 11-bit Identifier (standard frame) is used (Xtd = '0'),
    }                                                   //it is programmed to ID[28:18]. In this case, ID[17:0] can be ignored.
    else
    {
        CanaShadow.CAN_IF2ARB.bit.ID = ((MsgID << 12 << 6) & 0x1FFC0000UL); //Standard  0  ID bit31-bit0,鏍囧噯甯х殑ID鏄湪bit28-bit18?  鎵╁睍甯D鏄�29浣嶏紝鎼為敊浜嗗悧锛�
    }
    CanaRegs.CAN_IF2ARB.all = CanaShadow.CAN_IF2ARB.all;

    CanaShadow.CAN_IF2MCTL.all      = 0x0;
    CanaShadow.CAN_IF2MCTL.bit.DLC  = 0x8;              //Data Length is 8
    CanaShadow.CAN_IF2MCTL.bit.EoB  = 0x1;              //End of Block The message object is a single message object or the last message object in a FIFO Buffer Block.
    CanaShadow.CAN_IF2MCTL.bit.RxIE = ~MsgKind;         //enable Receive Interrupt
    CanaShadow.CAN_IF2MCTL.bit.TxIE = MsgKind;          //enable Transmit Interrupt
    CanaRegs.CAN_IF2MCTL.all        = CanaShadow.CAN_IF2MCTL.all;

    CanaShadow.CAN_IF2CMD.all           = 0x0;
    CanaShadow.CAN_IF2CMD.bit.Arb       = 0x1;                      //Access Arbitration Bits
    CanaShadow.CAN_IF2CMD.bit.Control   = 0x1;
    CanaShadow.CAN_IF2CMD.bit.Mask      = 0x1;
    CanaShadow.CAN_IF2CMD.bit.DIR       = 0x1;                      //write
    CanaShadow.CAN_IF2CMD.bit.MSG_NUM   = MbNum;                    //閭缂栧彿
    CanaRegs.CAN_IF2CMD.all             = CanaShadow.CAN_IF2CMD.all;
}

/*!
 * @brief 瀵笴AN閭鐨処D銆佸彂閫佺被鍨嬨�佸抚绫诲瀷杩涜鍒濆鍖�
 * @param MbNum: 閭缂栧彿
 * @param MsgID: CAN ID
 * @param MsgKind:閭鏂瑰悜 鍙戦��
 * @param FrameKind:甯х被鍨�
 * @return 鏃�
 */
static void ConfigureMailBoxTransmit(Uint16 MbNum, Uint32 MsgID, Uint16 MsgKind, Uint16 FrameKind)    //閰嶇疆鍙戦�丮B锛屾病鏈夋帴鏀�
{
    struct CAN_REGS CanaShadow;

    while (CanaRegs.CAN_IF1CMD.bit.Busy == 0x1u)
    {

    }

    CanaShadow.CAN_IF1MSK.all   = 0x0;          //
    CanaRegs.CAN_IF1MSK.all     = CanaShadow.CAN_IF1MSK.all;

    CanaShadow.CAN_IF1ARB.all           = 0x0;
    CanaShadow.CAN_IF1ARB.bit.Dir       = MsgKind;          //1 for Recieve
    CanaShadow.CAN_IF1ARB.bit.MsgVal    = MB_ENABLE;        //enable the Message Object  Message Valid
    CanaShadow.CAN_IF1ARB.bit.Xtd       = FrameKind;        //Standard/Extend frame
    if (0u != FrameKind)
    {
        CanaShadow.CAN_IF1ARB.bit.ID = MsgID;           //Extend frame  1     If an 11-bit Identifier (standard frame) is used (Xtd = '0'),
    }                                                   //it is programmed to ID[28:18]. In this case, ID[17:0] can be ignored.
    else
    {
        CanaShadow.CAN_IF1ARB.bit.ID = ((MsgID << 12 << 6) & 0x1FFC0000UL); //Standard  0  ID bit31-bit0,鏍囧噯甯х殑ID鏄湪bit28-bit18?  鎵╁睍甯D鏄�29浣嶏紝鎼為敊浜嗗悧锛�
    }
    CanaRegs.CAN_IF1ARB.all = CanaShadow.CAN_IF1ARB.all;

    CanaShadow.CAN_IF1MCTL.all      = 0x0;
    CanaShadow.CAN_IF1MCTL.bit.DLC  = 0x8;              //Data Length is 8
    CanaShadow.CAN_IF1MCTL.bit.EoB  = 0x1;              //End of Block The message object is a single message object or the last message object in a FIFO Buffer Block.
    CanaShadow.CAN_IF1MCTL.bit.RxIE = ~MsgKind;         //enable Receive Interrupt
    CanaShadow.CAN_IF1MCTL.bit.TxIE = ~MsgKind;          //Disable Transmit Interrupt
    CanaRegs.CAN_IF1MCTL.all        = CanaShadow.CAN_IF1MCTL.all;

    CanaShadow.CAN_IF1CMD.all           = 0x0;
    CanaShadow.CAN_IF1CMD.bit.Arb       = 0x1;                      //Access Arbitration Bits
    CanaShadow.CAN_IF1CMD.bit.Control   = 0x1;
    CanaShadow.CAN_IF1CMD.bit.Mask      = 0x1;
    CanaShadow.CAN_IF1CMD.bit.DIR       = 0x1;                      //write
    CanaShadow.CAN_IF1CMD.bit.MSG_NUM   = MbNum;                    //閭缂栧彿
    CanaRegs.CAN_IF1CMD.all             = CanaShadow.CAN_IF1CMD.all;
}

/* ***************************************************************鍏ㄥ眬鍑芥暟瀹氫箟*********************************************************/
/*!
 * @brief  鍒濆鍖栭偖绠辨湇鍔�
 * @param  鏃�
 * @retval 鏃�
 */
void InitMailBoxService(void)
{
    /*************************************************************************
     * *閰嶇疆CAN鍙戦�佹帴鏀堕偖绠�
     ***************************************************************************/
    CELL_NUM CELL=0;
    int i=(Uint16)CELL;

    ConfigureMailBox(CANMB_GUI_SET, CANID_GUI_SET_0+16*i, CANRXENABLEBIT, STANDARDFRAME);
    DELAY_US(10);

    ConfigureMailBox(CANMB_GUIOL_SET, CANID_GUIOL_SET, CANRXENABLEBIT, STANDARDFRAME);
    DELAY_US(10);

    ConfigureMailBox(CANMB_CloseCell_SET, CANID_CloseCell_SET, CANRXENABLEBIT, STANDARDFRAME);
    DELAY_US(10);

    if(i==0||i==1||i==2)//鎺ユ敹鏈熸湜鍔熺巼鐨勯偖绠�
    {
        ConfigureMailBox(CANMB_BBLLC_CELL_0, CANID_BBLLC_CELL_3, CANRXENABLEBIT, STANDARDFRAME);
        DELAY_US(10);
    }
    if(i==0||i==1||i==2)//鍙戦�佺洰鍓嶇數娴�
    {
        ConfigureMailBoxTransmit(CANMB_BBLLC_CELL_1, CANID_BBLLC_CELL_0, CANTXENABLEBIT, STANDARDFRAME);
        DELAY_US(10);
    }
    if(i==3)//鎺ユ敹鐩墠鐢垫祦
    {
        ConfigureMailBox(CANMB_BBLLC_CELL_2, CANID_BBLLC_CELL_0, CANRXENABLEBIT, STANDARDFRAME);
        DELAY_US(10);
    }


    if(i==3)
    {
        ConfigureMailBoxTransmit(CANMB_BBLLC_CELL_3, CANID_BBLLC_CELL_3, CANTXENABLEBIT, STANDARDFRAME);
        DELAY_US(10);
    }
    ConfigureMailBoxTransmit(CANMB_BBLLC_MONITORS_MESSAGE, CANID_BBLLC_MONITORS_MESSAGE_0+16*i, CANTXENABLEBIT,STANDARDFRAME);
    DELAY_US(10);

    ConfigureMailBoxTransmit(CANMB_BBLLC_MONITOR_POWER, CANID_BBLLC_MONITOR_POWER_0+16*i, CANTXENABLEBIT,STANDARDFRAME);
    DELAY_US(10);

    ConfigureMailBoxTransmit(CANMB_BBLLC_MONITOR_WARNING, CANID_BBLLC_MONITOR_WARNING, CANTXENABLEBIT,STANDARDFRAME);
    DELAY_US(10);

    ConfigureMailBoxTransmit(CANMB_BBLLC_TEMP, CANID_BBLLC_TEMP_0+16*i, CANTXENABLEBIT,STANDARDFRAME);
    DELAY_US(10);

}

/*!
 * @brief 鎵цCAN鏁版嵁鍙戦��
 * @param MSG_NUM:閭缂栧彿
 * @param MSG:瑁呭～瀹屾垚寰呭彂閫佺殑鏁版嵁
 * @return 鏃�
 */
void SendCANMessage(Uint16 MSG_NUM, CAN_Data_Type MSG)
{
    struct CAN_REGS CanaShadow2;

    while (CanaRegs.CAN_IF1CMD.bit.Busy == 1u)
    {
    }

    CanaRegs.CAN_IF1DATA.bit.Data_0 = (MSG.BYTE0 & 0xFFu);
    CanaRegs.CAN_IF1DATA.bit.Data_1 = (MSG.BYTE1 & 0xFFu);
    CanaRegs.CAN_IF1DATA.bit.Data_2 = (MSG.BYTE2 & 0xFFu);
    CanaRegs.CAN_IF1DATA.bit.Data_3 = (MSG.BYTE3 & 0xFFu);
    CanaRegs.CAN_IF1DATB.bit.Data_4 = (MSG.BYTE4 & 0xFFu);
    CanaRegs.CAN_IF1DATB.bit.Data_5 = (MSG.BYTE5 & 0xFFu);
    CanaRegs.CAN_IF1DATB.bit.Data_6 = (MSG.BYTE6 & 0xFFu);
    CanaRegs.CAN_IF1DATB.bit.Data_7 = (MSG.BYTE7 & 0xFFu);

    CanaShadow2.CAN_IF1CMD.all          = 0x0;
    CanaShadow2.CAN_IF1CMD.bit.Control  = 1;
    CanaShadow2.CAN_IF1CMD.bit.DIR      = 0x1;  //write
    CanaShadow2.CAN_IF1CMD.bit.MSG_NUM  = MSG_NUM;

    CanaShadow2.CAN_IF1CMD.bit.DATA_A   = 0x1;  //byte0-3 浠嶪F1/2浼犳暟鎹埌Mailbox
    CanaShadow2.CAN_IF1CMD.bit.DATA_B   = 0x1;  //byte4-7 浠嶪F1/2浼犳暟鎹埌Mailbox

    CanaShadow2.CAN_IF1CMD.bit.TXRQST   = 0x1;  //Sets TxRqst/NewDat in message object.

    CanaRegs.CAN_IF1CMD.all = CanaShadow2.CAN_IF1CMD.all;

    while (CanaRegs.CAN_IF1CMD.bit.Busy == 1u)
    {
    }
}

/*!
 * @brief CAN鎺ユ敹銆佸彂閫佷腑鏂嚱鏁�
 * @param 鏃�
 * @return 鏃�
 */
__interrupt void Cana0ISR(void)   //涓柇鍚戦噺琛ㄤ腑9.5鐨処SR  main鍑芥暟璋冪敤receive涓殑鍑芥暟锛屼娇鐢� 0x200鏁版嵁 OBCMsg鎺ユ敹鐨勪俊鎭紝OBCMsg鏄繖涓狪SR绋嬪簭case CANMB_NXP_REQUEST涓殑CanaRegs.CAN_IF2DATA鍐欏叆鐨�
{
    Uint32 status, status0;
    struct CAN_REGS CanaShadow2;
    status = CanaRegs.CAN_INT.bit.INT0ID;//绮剧粏鐐癸紝璇籌NT0ID灏辫锛屼笉鐢ㄥ叏璇�

    switch (status)
    {
    case CANMsg_ErrorCode:  //can鏁呴殰涓嶅仛澶勭悊
    {
        status0 = CanaRegs.CAN_ES.all;
    }
        break;
    case CANMB_GUI_SET:    //0x704U
    {
        /* Wait for busy bit to clear*/
        while (CanaRegs.CAN_IF2CMD.bit.Busy == 1u)
        {
        }
        /* Set the Message Data A, Data B, and control values to be read
         * on request for data from the message object.
         * Transfer the message object to the message object IF register.*/
        CanaShadow2.CAN_IF2CMD.all          = 0x0;
        CanaShadow2.CAN_IF2CMD.bit.DATA_A   = 0x1;
        CanaShadow2.CAN_IF2CMD.bit.DATA_B   = 0x1;
        CanaShadow2.CAN_IF2CMD.bit.Control  = 0x1;
        CanaShadow2.CAN_IF2CMD.bit.MSG_NUM  = CANMB_GUI_SET;
        CanaRegs.CAN_IF2CMD.all             = CanaShadow2.CAN_IF2CMD.all;

        /* Wait for busy bit to clear*/
        while (CanaRegs.CAN_IF2CMD.bit.Busy == 1u)
        {
        }

        /* Read out the IF control Register.*/
        if (CanaRegs.CAN_IF2MCTL.bit.NewDat == 0x1u)
        {
            if(true == GUIMsgWriteEnableFlag)
            {
                GUIMsgWriteEnableFlag = false;
                GUIMsg.BYTE0 = CanaRegs.CAN_IF2DATA.bit.Data_0;
                GUIMsg.BYTE1 = CanaRegs.CAN_IF2DATA.bit.Data_1;
                GUIMsg.BYTE2 = CanaRegs.CAN_IF2DATA.bit.Data_2;
                GUIMsg.BYTE3 = CanaRegs.CAN_IF2DATA.bit.Data_3;
                GUIMsg.BYTE4 = CanaRegs.CAN_IF2DATB.bit.Data_4;
                GUIMsg.BYTE5 = CanaRegs.CAN_IF2DATB.bit.Data_5;
                GUIMsg.BYTE6 = CanaRegs.CAN_IF2DATB.bit.Data_6;
                GUIMsg.BYTE7 = CanaRegs.CAN_IF2DATB.bit.Data_7;
                GUIMsgReadEnableFlag = true;
            }

            CanaShadow2.CAN_IF2CMD.all          = 0x0;
            CanaShadow2.CAN_IF2CMD.bit.TxRqst   = 0x1;
            CanaShadow2.CAN_IF2CMD.bit.MSG_NUM  = CANMB_GUI_SET;
            CanaRegs.CAN_IF2CMD.all             = CanaShadow2.CAN_IF2CMD.all;
            while (CanaRegs.CAN_IF2CMD.bit.Busy == 1u)
            {
            }
        }
        while (CanaRegs.CAN_IF2CMD.bit.Busy == 1u)
        {
        }
        CanaShadow2.CAN_IF2CMD.all              = 0x0;
        CanaShadow2.CAN_IF2CMD.bit.ClrIntPnd    = 0x1;
        CanaShadow2.CAN_IF2CMD.bit.MSG_NUM      = CANMB_GUI_SET;
        CanaRegs.CAN_IF2CMD.all                 = CanaShadow2.CAN_IF2CMD.all;
        while (CanaRegs.CAN_IF2CMD.bit.Busy == 1u)
        {
        }
    }
        break;
    case CANMB_BBLLC_CELL_0:    //0x704U
     {
         /* Wait for busy bit to clear*/
         while (CanaRegs.CAN_IF2CMD.bit.Busy == 1u)
         {
         }
         /* Set the Message Data A, Data B, and control values to be read
          * on request for data from the message object.
          * Transfer the message object to the message object IF register.*/
         CanaShadow2.CAN_IF2CMD.all          = 0x0;
         CanaShadow2.CAN_IF2CMD.bit.DATA_A   = 0x1;
         CanaShadow2.CAN_IF2CMD.bit.DATA_B   = 0x1;
         CanaShadow2.CAN_IF2CMD.bit.Control  = 0x1;
         CanaShadow2.CAN_IF2CMD.bit.MSG_NUM  = CANMB_BBLLC_CELL_0;
         CanaRegs.CAN_IF2CMD.all             = CanaShadow2.CAN_IF2CMD.all;

         /* Wait for busy bit to clear*/
         while (CanaRegs.CAN_IF2CMD.bit.Busy == 1u)
         {
         }

         /* Read out the IF control Register.*/
         if (CanaRegs.CAN_IF2MCTL.bit.NewDat == 0x1u)
         {
             if(true == CELLMsgWriteEnableFlag)
             {
                 CELLMsgWriteEnableFlag = false;
                 CELL3Msg.BYTE0 = CanaRegs.CAN_IF2DATA.bit.Data_0;
                 CELL3Msg.BYTE1 = CanaRegs.CAN_IF2DATA.bit.Data_1;
                 CELL3Msg.BYTE2 = CanaRegs.CAN_IF2DATA.bit.Data_2;
                 CELL3Msg.BYTE3 = CanaRegs.CAN_IF2DATA.bit.Data_3;
                 CELL3Msg.BYTE4 = CanaRegs.CAN_IF2DATB.bit.Data_4;
                 CELL3Msg.BYTE5 = CanaRegs.CAN_IF2DATB.bit.Data_5;
                 CELL3Msg.BYTE6 = CanaRegs.CAN_IF2DATB.bit.Data_6;
                 CELL3Msg.BYTE7 = CanaRegs.CAN_IF2DATB.bit.Data_7;
                 CELL3MsgReadEnableFlag  = true;
             }

             CanaShadow2.CAN_IF2CMD.all          = 0x0;
             CanaShadow2.CAN_IF2CMD.bit.TxRqst   = 0x1;
             CanaShadow2.CAN_IF2CMD.bit.MSG_NUM  = CANMB_BBLLC_CELL_0;
             CanaRegs.CAN_IF2CMD.all             = CanaShadow2.CAN_IF2CMD.all;
             while (CanaRegs.CAN_IF2CMD.bit.Busy == 1u)
             {
             }
         }
         while (CanaRegs.CAN_IF2CMD.bit.Busy == 1u)
         {
         }
         CanaShadow2.CAN_IF2CMD.all              = 0x0;
         CanaShadow2.CAN_IF2CMD.bit.ClrIntPnd    = 0x1;
         CanaShadow2.CAN_IF2CMD.bit.MSG_NUM      = CANMB_BBLLC_CELL_0;
         CanaRegs.CAN_IF2CMD.all                 = CanaShadow2.CAN_IF2CMD.all;
         while (CanaRegs.CAN_IF2CMD.bit.Busy == 1u)
         {
         }
     }
         break;
    case CANMB_GUIOL_SET:    //0x704U
    {
        /* Wait for busy bit to clear*/
        while (CanaRegs.CAN_IF2CMD.bit.Busy == 1u)
        {
        }
        /* Set the Message Data A, Data B, and control values to be read
         * on request for data from the message object.
         * Transfer the message object to the message object IF register.*/
        CanaShadow2.CAN_IF2CMD.all          = 0x0;
        CanaShadow2.CAN_IF2CMD.bit.DATA_A   = 0x1;
        CanaShadow2.CAN_IF2CMD.bit.DATA_B   = 0x1;
        CanaShadow2.CAN_IF2CMD.bit.Control  = 0x1;
        CanaShadow2.CAN_IF2CMD.bit.MSG_NUM  = CANMB_GUIOL_SET;
        CanaRegs.CAN_IF2CMD.all             = CanaShadow2.CAN_IF2CMD.all;

        /* Wait for busy bit to clear*/
        while (CanaRegs.CAN_IF2CMD.bit.Busy == 1u)
        {
        }

        /* Read out the IF control Register.*/
        if (CanaRegs.CAN_IF2MCTL.bit.NewDat == 0x1u)
        {
            if(true == GUIOLMsgWriteEnableFlag)
            {
                GUIOLMsgWriteEnableFlag = false;
                OLMsg.BYTE0 = CanaRegs.CAN_IF2DATA.bit.Data_0;
                OLMsg.BYTE1 = CanaRegs.CAN_IF2DATA.bit.Data_1;
                OLMsg.BYTE2 = CanaRegs.CAN_IF2DATA.bit.Data_2;
                OLMsg.BYTE3 = CanaRegs.CAN_IF2DATA.bit.Data_3;
                OLMsg.BYTE4 = CanaRegs.CAN_IF2DATB.bit.Data_4;
                OLMsg.BYTE5 = CanaRegs.CAN_IF2DATB.bit.Data_5;
                OLMsg.BYTE6 = CanaRegs.CAN_IF2DATB.bit.Data_6;
                OLMsg.BYTE7 = CanaRegs.CAN_IF2DATB.bit.Data_7;
                GUIOLMsgReadEnableFlag = true;
            }

            CanaShadow2.CAN_IF2CMD.all          = 0x0;
            CanaShadow2.CAN_IF2CMD.bit.TxRqst   = 0x1;
            CanaShadow2.CAN_IF2CMD.bit.MSG_NUM  = CANMB_GUIOL_SET;
            CanaRegs.CAN_IF2CMD.all             = CanaShadow2.CAN_IF2CMD.all;
            while (CanaRegs.CAN_IF2CMD.bit.Busy == 1u)
            {
            }
        }
        while (CanaRegs.CAN_IF2CMD.bit.Busy == 1u)
        {
        }
        CanaShadow2.CAN_IF2CMD.all              = 0x0;
        CanaShadow2.CAN_IF2CMD.bit.ClrIntPnd    = 0x1;
        CanaShadow2.CAN_IF2CMD.bit.MSG_NUM      = CANMB_GUIOL_SET;
        CanaRegs.CAN_IF2CMD.all                 = CanaShadow2.CAN_IF2CMD.all;
        while (CanaRegs.CAN_IF2CMD.bit.Busy == 1u)
        {
        }
    }
        break;
    case CANMB_CloseCell_SET:    //0x705U
    {
        /* Wait for busy bit to clear*/
        while (CanaRegs.CAN_IF2CMD.bit.Busy == 1u)
        {
        }
        /* Set the Message Data A, Data B, and control values to be read
         * on request for data from the message object.
         * Transfer the message object to the message object IF register.*/
        CanaShadow2.CAN_IF2CMD.all          = 0x0;
        CanaShadow2.CAN_IF2CMD.bit.DATA_A   = 0x1;
        CanaShadow2.CAN_IF2CMD.bit.DATA_B   = 0x1;
        CanaShadow2.CAN_IF2CMD.bit.Control  = 0x1;
        CanaShadow2.CAN_IF2CMD.bit.MSG_NUM  = CANMB_CloseCell_SET;
        CanaRegs.CAN_IF2CMD.all             = CanaShadow2.CAN_IF2CMD.all;

        /* Wait for busy bit to clear*/
        while (CanaRegs.CAN_IF2CMD.bit.Busy == 1u)
        {
        }

        /* Read out the IF control Register.*/
        if (CanaRegs.CAN_IF2MCTL.bit.NewDat == 0x1u)
        {
            if(true == CloseCellMsgWriteEnableFlag)
            {
                CloseCellMsgWriteEnableFlag = false;
                CloseMsg.BYTE0 = CanaRegs.CAN_IF2DATA.bit.Data_0;
                CloseMsg.BYTE1 = CanaRegs.CAN_IF2DATA.bit.Data_1;
                CloseMsg.BYTE2 = CanaRegs.CAN_IF2DATA.bit.Data_2;
                CloseMsg.BYTE3 = CanaRegs.CAN_IF2DATA.bit.Data_3;
                CloseMsg.BYTE4 = CanaRegs.CAN_IF2DATB.bit.Data_4;
                CloseMsg.BYTE5 = CanaRegs.CAN_IF2DATB.bit.Data_5;
                CloseMsg.BYTE6 = CanaRegs.CAN_IF2DATB.bit.Data_6;
                CloseMsg.BYTE7 = CanaRegs.CAN_IF2DATB.bit.Data_7;
                CloseCellMsgReadEnableFlag = true;
            }

            CanaShadow2.CAN_IF2CMD.all          = 0x0;
            CanaShadow2.CAN_IF2CMD.bit.TxRqst   = 0x1;
            CanaShadow2.CAN_IF2CMD.bit.MSG_NUM  = CANMB_CloseCell_SET;
            CanaRegs.CAN_IF2CMD.all             = CanaShadow2.CAN_IF2CMD.all;
            while (CanaRegs.CAN_IF2CMD.bit.Busy == 1u)
            {
            }
        }
        while (CanaRegs.CAN_IF2CMD.bit.Busy == 1u)
        {
        }
        CanaShadow2.CAN_IF2CMD.all              = 0x0;
        CanaShadow2.CAN_IF2CMD.bit.ClrIntPnd    = 0x1;
        CanaShadow2.CAN_IF2CMD.bit.MSG_NUM      = CANMB_CloseCell_SET;
        CanaRegs.CAN_IF2CMD.all                 = CanaShadow2.CAN_IF2CMD.all;
        while (CanaRegs.CAN_IF2CMD.bit.Busy == 1u)
        {
        }
    }
        break;
    case CANMB_BBLLC_CELL_2:    //0x705U
    {
        /* Wait for busy bit to clear*/
        while (CanaRegs.CAN_IF2CMD.bit.Busy == 1u)
        {
        }
        /* Set the Message Data A, Data B, and control values to be read
         * on request for data from the message object.
         * Transfer the message object to the message object IF register.*/
        CanaShadow2.CAN_IF2CMD.all          = 0x0;
        CanaShadow2.CAN_IF2CMD.bit.DATA_A   = 0x1;
        CanaShadow2.CAN_IF2CMD.bit.DATA_B   = 0x1;
        CanaShadow2.CAN_IF2CMD.bit.Control  = 0x1;
        CanaShadow2.CAN_IF2CMD.bit.MSG_NUM  = CANMB_BBLLC_CELL_2;
        CanaRegs.CAN_IF2CMD.all             = CanaShadow2.CAN_IF2CMD.all;

        /* Wait for busy bit to clear*/
        while (CanaRegs.CAN_IF2CMD.bit.Busy == 1u)
        {
        }

        /* Read out the IF control Register.*/
        if (CanaRegs.CAN_IF2MCTL.bit.NewDat == 0x1u)
        {
            if(true == CellCurrMsgWriteEnableFlag)
            {
                CellCurrMsgWriteEnableFlag = false;
                CurrMsg.BYTE0 = CanaRegs.CAN_IF2DATA.bit.Data_0;
                CurrMsg.BYTE1 = CanaRegs.CAN_IF2DATA.bit.Data_1;
                CurrMsg.BYTE2 = CanaRegs.CAN_IF2DATA.bit.Data_2;
                CurrMsg.BYTE3 = CanaRegs.CAN_IF2DATA.bit.Data_3;
                CurrMsg.BYTE4 = CanaRegs.CAN_IF2DATB.bit.Data_4;
                CurrMsg.BYTE5 = CanaRegs.CAN_IF2DATB.bit.Data_5;
                CurrMsg.BYTE6 = CanaRegs.CAN_IF2DATB.bit.Data_6;
                CurrMsg.BYTE7 = CanaRegs.CAN_IF2DATB.bit.Data_7;
                CellCurrMsgReadEnableFlag = true;
            }

            CanaShadow2.CAN_IF2CMD.all          = 0x0;
            CanaShadow2.CAN_IF2CMD.bit.TxRqst   = 0x1;
            CanaShadow2.CAN_IF2CMD.bit.MSG_NUM  = CANMB_BBLLC_CELL_2;
            CanaRegs.CAN_IF2CMD.all             = CanaShadow2.CAN_IF2CMD.all;
            while (CanaRegs.CAN_IF2CMD.bit.Busy == 1u)
            {
            }
        }
        while (CanaRegs.CAN_IF2CMD.bit.Busy == 1u)
        {
        }
        CanaShadow2.CAN_IF2CMD.all              = 0x0;
        CanaShadow2.CAN_IF2CMD.bit.ClrIntPnd    = 0x1;
        CanaShadow2.CAN_IF2CMD.bit.MSG_NUM      = CANMB_BBLLC_CELL_2;
        CanaRegs.CAN_IF2CMD.all                 = CanaShadow2.CAN_IF2CMD.all;
        while (CanaRegs.CAN_IF2CMD.bit.Busy == 1u)
        {
        }
    }
        break;
    default:
        /**/
        break;
    }

    CanaShadow2.CAN_GLB_INT_CLR.bit.INT0_FLG_CLR = 0x1; //涓柇0娓協lag
//    CanaShadow2.CAN_GLB_INT_CLR.bit.INT1_FLG_CLR = 0x1;
    CanaRegs.CAN_GLB_INT_CLR.all = CanaShadow2.CAN_GLB_INT_CLR.all;

    PieCtrlRegs.PIEACK.all = PIEACK_GROUP9;
}





__interrupt void EPWM7CAN_ISR(void)
{
    volatile struct CAN_REGS CanaShadow2;

    // 妫�鏌PWM7涓柇鏍囧織
    if (EPwm7Regs.ETFLG.bit.INT == 1)
    {


    }
    /*----- 涓柇娓呯悊 -----*/
    EPwm7Regs.ETCLR.bit.INT = 1;       // 娓呴櫎EPWM涓柇鏍囧織
    PieCtrlRegs.PIEACK.all = PIEACK_GROUP3; // 淇涓烘纭殑PIE缁�
}
/*!
 * @brief  鎷疯礉GUIMsgCAN鏁版嵁
 * @param  CAN_Data_Type绫诲瀷鎸囬拡
 * @retval 鏃�
 */
void GetGUIMsgCANData(CAN_Data_Type *p)
{
    memcpy(p, &GUIMsg, sizeof(CAN_Data_Type));
}

void GetCELL3MsgCANData(CAN_Data_Type *p)
{
    memcpy(p, &CELL3Msg, sizeof(CAN_Data_Type));
}
void GetOLMsgCANData(CAN_Data_Type *p)
{
    memcpy(p, &OLMsg, sizeof(CAN_Data_Type));
}
void GetCloseCellMsgCANData(CAN_Data_Type *p)
{
    memcpy(p, &CloseMsg, sizeof(CAN_Data_Type));
}
void GetCurrMsgCANData(CAN_Data_Type *p)
{
    memcpy(p, &CurrMsg, sizeof(CAN_Data_Type));
}
/*!
 * @brief  浣胯兘GUIMsg鍐欐搷浣�
 * @param  鏃�
 * @retval 鏃�
 */
void EnableGUIMsgWrite(void)
{
     GUIMsgWriteEnableFlag = true;
}
void EnableCELLMsgWrite(void)
{
     CELLMsgWriteEnableFlag = true;
}
void EnableGUIOLMsgWrite(void)
{
     GUIOLMsgWriteEnableFlag = true;
}
void EnableCloseCellMsgWrite(void)
{
     CloseCellMsgWriteEnableFlag = true;
}
void EnableCurrMsgWrite(void)
{
    CellCurrMsgWriteEnableFlag = true;
}
/*!
 * @brief  绂佺敤GUIMsg璇绘搷浣�
 * @param  鏃�
 * @retval 鏃�
 */
void DisableGUIMsgRead(void)
{
    GUIMsgReadEnableFlag = false;
}

void DisableCELL3MsgRead(void)
{
    CELL3MsgReadEnableFlag = false;
}
void DisableGUIOLMsgRead(void)
{
    GUIOLMsgReadEnableFlag = false;
}
void DisableCloseCellMsgRead(void)
{
    CloseCellMsgReadEnableFlag = false;
}
void DisableCurrMsgRead(void)
{
    CellCurrMsgReadEnableFlag = false;
}
/*!
 * @brief  鑾峰彇GUIMsg璇讳娇鑳芥爣蹇�
 * @param  鏃�
 * @retval 杩斿洖鏍囧織鍊�
 */
bool GetGUIMsgReadEnableFlag(void)
{
    return GUIMsgReadEnableFlag;
}
bool GetCELL3MsgReadEnableFlag(void)
{
    return CELL3MsgReadEnableFlag;
}
bool GetGUIOLMsgReadEnableFlag(void)
{
    return GUIOLMsgReadEnableFlag;
}
bool GetCloseCellMsgReadEnableFlag(void)
{
    return CloseCellMsgReadEnableFlag;
}
bool CurrMsgReadEnableFlag(void)
{
    return CellCurrMsgReadEnableFlag;
}
